import { messages } from 'share/common';
import Space from 'components/space';
import React from 'react';
import menuRoute from 'routes/menuRoute';
import { newKey2OldId } from 'containers/supplier-management/invoice-management/dataSources';
import ConfigurationCenterService from 'containers/setting/configuration-center/configuration-center.service';
import configureStore from 'stores/index';
import { Tooltip } from 'antd';
import Info from "components/info";

class JumpConfigCenter extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      oldConfig: {},
      visible: true
    };
  }

  jump = () => {
    let url = menuRoute.getPageUrlByKey('configuration-center');
    this.context.router.push(url);
  };

  componentDidMount() {
    const { tab: key } = this.props;
    let oldConfigs = [];
    if (newKey2OldId.has(key)) {
      for (let i of newKey2OldId.get(key)) {
        oldConfigs.push(this.getOldConfig(i));
      }
      Promise.all(oldConfigs)
        .finally(() => {
          setTimeout(() => {
            this.setState({ visible: false });
          }, 2000);
        });
    }
  }

  getOldConfig = async (id) => { // 获取老的配置中心数据， 以此来判断是否要展示一个按钮提示
    const { oldConfig } = this.state;
    const menuPermission = configureStore.store.getState().main.menuPermission || {};
    if (!menuPermission.tenantId) {
      return;
    }
    const params = {
      'page': 0,
      'size': 1000,
      'itemIds': [id],
      'tenantId': menuPermission.tenantId,
      'getDetail': 'Y',
      'advancedRule': true
    };
    const ret = await ConfigurationCenterService.getConfigurationList(params);
    oldConfig[id] = !!ret.data.rows[0].configRules[0];
    this.setState({ oldConfig });
  };


  handleClose = () => {
    this.setState({ visible: false });
  };

  handleShow = () => {
    this.setState({ visible: true });
  };


  render() {
    const { oldConfig, visible } = this.state;
    const isShowPop = Object.keys(oldConfig).some(item => oldConfig[item]) && visible; // 有数据存在即可显示popover
    return (
      <Space direction="vertical" style={this.props.style}>
        <Info
          paragraph
          attributes={[{title: messages('expense-3.key200')/*温馨提示*/, dataIndex: 'tip', span: 24, widthFix: false}]}
          dataSource={{tip: messages('expense-3.key201')/*【高级设置】的配置，不会同步显示到此页面；此页面配置的规则会覆盖【高级设置】的配置*/}}
        />
        <Tooltip placement="top" title={messages('expense-3.key202')/*请移步【高级设置】页面管理规则*/} visible={isShowPop} arrowPointAtCenter>
          <a onMouseEnter={this.handleShow} onMouseLeave={this.handleClose} ref={'settingBtn'} onClick={this.jump}>{messages('expense-3.key203')/*更多高级设置，前往查看&设置 >*/}</a>
        </Tooltip>
      </Space>
    );
  }
}


JumpConfigCenter.contextTypes = {
  router: React.PropTypes.object
};


export default JumpConfigCenter;
